﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HYSoft.Web
{
    public partial class permissions: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["uid"] == null || Session["uid"] == "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
            if (Request.QueryString["touid"] == null)
                uidnull();
            else
            {
                if (!IsPostBack)
                {
                    user_list();
                    data_list();
                    user_status();
                }
                nodetype_change();
            }
        }

        private void user_list()
        {
            string units1 = "";
            Model.user users = new BLL.user().GetModel((int)Session["uid"]);
            if (users != null)
            {
                units1 = users.unit;
            }


            string sql = " [unit]='" + units1 + "' and [type]=0";
            DataSet ds = new BLL.user().GetList(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DropDownList4.DataSource = ds.Tables[0].DefaultView;
                DropDownList4.DataTextField = "name";
                DropDownList4.DataValueField = "index";
                DropDownList4.DataBind();
                ds.Clear();
                ds.Dispose();
                DropDownList4.SelectedValue = Request.QueryString["touid"].ToString();
            }
        }

        private void user_status()
        {
            int uid2 = int.Parse(Request.QueryString["touid"].ToString());
            Model.user us = new BLL.user().GetModel(uid2);
            if (us != null)
            {
                DropDownList1.SelectedIndex = (Int32)us.intvalue1;
                DropDownList2.SelectedIndex = (Int32)us.intvalue2;
            }
        }

        private void uidnull()
        {
            string units1 = "";
            //通过登陆用户的Index（uid）得到所在单位unit
            Model.user us = new BLL.user().GetModel((Int32)Session["uid"]);
            if (us != null)
            {
                units1 = us.unit;
            }
            //获取该单位下读写权限type为0的普通用户，然后再将已经查找到得第一个值传递过去
            string sql = " [unit]='" + units1 + "' and [type]=0";
            DataSet ds = new BLL.user().GetList(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Response.Write("<script>window.location = 'permissions.aspx?touid=" + ds.Tables[0].Rows[0]["index"] + "';</script>");
            }
            else
            {
                Response.Write("<script>alert('该单位还没有已注册普通用户！');history.back();</script>");
            }
        }

        protected void nodetype_change()
        {
            string nodetype = DropDownList4.SelectedValue.ToString();
            if (nodetype != Request.QueryString["touid"].ToString())
                Response.Write("<script>window.location = 'permissions.aspx?touid=" + nodetype + "';</script>");
        }


        protected void data_list()
        {
            int uid2 = int.Parse(Request.QueryString["touid"].ToString());
            string sql = " [index]=" + uid2 + "";
            DataSet ds = new BLL.user().GetList(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Repeater1.DataSource = ds.Tables[0];
                Repeater1.DataBind();
            }
            ds.Clear();
            ds.Dispose();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int uid2 = int.Parse(Request.QueryString["touid"].ToString());
            Model.user us = new BLL.user().GetModel(uid2);
            us.intvalue1 = DropDownList1.SelectedIndex;
            us.intvalue2 = DropDownList2.SelectedIndex;
            if (new BLL.user().Update(us))
            {
                Response.Write("<script>alert('用户状态修改成功！');window.location = 'permissions.aspx?touid=" + uid2 + "';</script>");
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            int uid2 = int.Parse(Request.QueryString["touid"].ToString());
            Response.Write("<script>alert('已放弃用户状态修改！');window.location = 'permissions.aspx?touid=" + uid2 + "';</script>");
        }
    }
}